package com.lagou.edu.utils;

import com.lagou.edu.annotation.Autowired;
import com.lagou.edu.annotation.Service;
import com.lagou.edu.service.TransferService;

import javax.sound.midi.Transmitter;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author sxnan
 * @className TransactionManager
 * @description TODO
 * @date 2020/6/6
 */
@Service
public class TransactionManager {

//    private TransactionManager(){}
//    private static TransactionManager transactionManager = new TransactionManager();
//    public static TransactionManager getInstance(){
//        return transactionManager;
//    }

    @Autowired
    private ConnectionUtils connectionUtils;

//    public void setConnectionUtils(ConnectionUtils connectionUtils) {
//        this.connectionUtils = connectionUtils;
//    }

    public void beginTransaction() throws SQLException {
        Connection currentThreadConnection = connectionUtils.getCurrentThreadConnection();
        currentThreadConnection.setAutoCommit(false);
    }

    public void commit() throws SQLException {
        connectionUtils.getCurrentThreadConnection().commit();
    }

    public void rollback() throws SQLException {
        connectionUtils.getCurrentThreadConnection().rollback();
    }
}
